"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 故障&可靠性测试
Case Name   : 修改主备数据库port信息不一致，主备功能正常
Create At   : 2021/02/18
@zou_jialiang0505328126
Description :
    1、修改主机端口
    2、修改备机端口（与主机不一致）
    3、gs_om -t stop&&gs_om -t start
    4、gsql分别连接主备机，主备库功能正常
Expect      :
    1、修改成功
    2、修改成功
    3、重启成功
    4、连接主备机后，主机进行事务操作，备机数据同步；
History     :
    modified by @wan005 at 2022.03.01:优化连接失败断言信息
"""

import unittest
from yat.test import macro
from yat.test import Node
from testcase.utils.CommonSH import CommonSH
from testcase.utils.CommonSH import RestartDbCluster
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class Ddldatabase(unittest.TestCase):
    commonshpri = CommonSH('PrimaryDbUser')
    commonshsta = CommonSH('Standby1DbUser')
    nodes_tuple = ('PrimaryDbUser', 'Standby1DbUser', 'Standby2DbUser')

    @RestartDbCluster(*nodes_tuple)
    def setUp(self):
        self.log = Logger()
        self.log.info('---Opengauss_Reliability_Dbsys_Case033.py 开始执行---')
        self.db_primary_user_node = Node(node='PrimaryDbUser')
        self.db_standby_user_node = Node(node='Standby1DbUser')
        self.constant = Constant()
        self.new_port = int(self.db_primary_user_node.db_port) + 100
        self.node_name = self.db_standby_user_node.sh('hostname').result()
        self.tblname = 'table033'

    def test_dbsys(self):
        self.log.info('-------------修改备机端口与主机不一致---------------')
        result = self.commonshsta.executeGsguc(
            'set', self.constant.GSGUC_SUCCESS_MSG,
            f'port={str(self.new_port)}', self.node_name)
        self.assertTrue(result)

        self.log.info('-------------重启数据库---------------')
        result = self.commonshpri.stopDbCluster()
        self.assertTrue(result)
        result = self.commonshpri.startDbCluster()
        self.assertTrue(result)

        self.log.info('------------gsql分别连接主备机，主备库功能正常----------------')
        self.log.info('======主节点使用原端口连接,创建表=======')
        result = self.commonshpri.executDbSql(
            f'create table {self.tblname}(i int);'
            f'insert into {self.tblname} values (1);')
        self.log.info(result)
        self.assertIn(self.constant.TABLE_CREATE_SUCCESS, result)
        self.log.info('======主节点使用新端口连接,查询=======')
        cmd = f'source {macro.DB_ENV_PATH};' \
            f'gsql -d {self.db_primary_user_node.db_name} ' \
            f'-p {self.new_port} -c "select * from {self.tblname}";'
        result = self.db_primary_user_node.sh(cmd).result()
        self.log.info(result)
        self.assertIn(self.constant.CONN_FAIL, result)
        self.log.info('======备节点使用原端口连接=======')
        result = self.commonshsta.executDbSql(
            f'select * from {self.tblname};')
        self.log.info(result)
        self.assertIn(self.constant.FAILED_CONNECT_DB, result)
        self.log.info('======备节点使用新端口连接,查询=======')
        cmd = f'source {macro.DB_ENV_PATH};' \
            f'gsql -d {self.db_primary_user_node.db_name} ' \
            f'-p {self.new_port} -c "select * from {self.tblname}";'
        result = self.db_standby_user_node.sh(cmd).result()
        self.log.info(result)
        self.assertIn('(1 row)', result)

    def tearDown(self):
        self.log.info('-----------删除表------------------')
        result = self.commonshpri.executDbSql(
            f'drop table if exists {self.tblname};')
        self.log.info(result)

        self.log.info('---------恢复端口------------------')
        result = self.commonshsta.executeGsguc(
            'set', self.constant.GSGUC_SUCCESS_MSG,
            f'port={self.db_primary_user_node.db_port}', self.node_name)
        self.log.info(result)
        result = self.commonshpri.stopDbCluster()
        self.log.info(result)
        result = self.commonshpri.startDbCluster()
        self.log.info(result)
        self.log.info('---Opengauss_Reliability_Dbsys_Case033.py 执行结束-------')
